var app = getApp()
var tempFilePaths;
var avatar=[]
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: '',
    tel: '',
    adress: '',
    serverinfo: '',
    disable: false,
    uploadImg: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let data = app.globalData.stallInfo
    this.setData(data)
    console.log(this.data)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  upload: function () {
    var that = this
    wx.chooseImage({
      count: 9, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'],
      success: function (res) {
        console.log(that.data.uploadImg)
        var num = that.data.imgNum
        var img = that.data.uploadImg
        tempFilePaths = res.tempFilePaths
        that.setData({
          uploadImg: img.concat(res.tempFilePaths)
        })
        wx.uploadFile({
          url: 'https://qili.docdic.com/api/file', //仅为示例，非真实的接口地址
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            avatar.push(res.data)
            console.log(avatar)
          }
        })
      }
    })
  },
  del_img: function (e) {
    let index = e.currentTarget.dataset.index
    let imgUrl = this.data.uploadImg
    imgUrl.splice(index, 1)
    console.log(index)
    console.log(imgUrl)
    this.setData({
      uploadImg: imgUrl
    })
  },
  formSubmit(e) {
    var that = this
    let url = ''
    let dName = e.detail.value.name
    let dTel = e.detail.value.tel
    let dAdress = e.detail.value.adress
    let dServerInfo = e.detail.value.serverinfo
    let logo = this.data.uploadImg
    if (dName == '') {
      wx.showToast({
        title: '请输入档口名字',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    if (!(/^1[34578]\d{9}$/.test(dTel))) {
      wx.showToast({
        title: '请输入正确的手机号码',
        icon: 'none',
        duration: 2000
      })
      return false
    }
    if (dAdress == '') {
      wx.showToast({
        title: '请输入档口地址',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    if (logo == '') {
      wx.showToast({
        title: '请上传图片',
        icon: 'success',
        duration: 2000
      })
      return false
    }
    let token = wx.getStorageSync('token')
    let userId = wx.getStorageSync('userId')
    let has_stall = wx.getStorageSync('has_stall')
    wx.uploadFile({
      url: 'https://qili.docdic.com/api/file', //仅为示例，非真实的接口地址
      filePath: tempFilePaths[0],
      name: 'file',
      success: function (res) {
        console.log(res)
        let params = {
          name: dName,
          phone: dTel,
          address: dAdress,
          logo: avatar
        }
        if (Number(has_stall) === 1) {
          url = 'https://qili.docdic.com/api/stall/' + that.data.id + '?token=' + token
          // url = 'https://qili.docdic.com/api/stall?token=' + token          
        } else {
          url = 'https://qili.docdic.com/api/stall?token=' + token
        }

        wx.request({
          url: url,
          method: 'post',
          data: params,
          header: {
            'content-type': 'application/x-www-form-urlencoded'
          },
          success: function (res) {
            console.log(res)
            if (Number(res.data.status === 201)) {
              wx.showToast({
                title: '上传成功',
                icon: 'success',
                duration: 1000
              })
              setTimeout(() => {
                wx.switchTab({
                  url: '../person',
                })
              }, 600)
            }
          },
          fail: function (err) {
            console.log(err)
            wx.showToast({
              title: '上传失败',
              icon: 'none',
              duration: 2000
            })
          }
        })
      }
    })
  }
})